package com.glory;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesServlet extends HelloServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream resourceAsStream1 = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        outPropertiesHtml(resp, resourceAsStream1);
        InputStream resourceAsStream2 = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/glory/aa.properties");
        outPropertiesHtml(resp, resourceAsStream2);

    }

    private void outPropertiesHtml(HttpServletResponse resp, InputStream resourceAsStream) throws IOException {
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        resp.getWriter().println(String.format("%s:%s",username,password));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
